#ifndef _EFR_H_
#define _EFR_H_

#include "mcu_regs.h"
#include "types.h"
#include "gpio.h"
#include "system.h"

#define SET_EFR_BIT(REG, BIT)           do { irq_disable();efr_change(REG, 0, BIT);irq_enable(); } while (0)

#define CLEAR_EFR_BIT(REG, BIT)         do { irq_disable();efr_change(REG, BIT, 0);irq_enable(); } while (0)

#define MODIFY_EFR_REG(REG, CLEARMASK, SETMASK)   do { irq_disable();efr_change(REG, CLEARMASK, SETMASK);irq_enable(); } while (0)

/*****  when read and write efr regester, advise operate during close EA interrupt  (IRQ_DISABLE()/IRQ_ENABLE())**/
#define READ_EFR_REG(REG)               (efr_read(REG))
#define READ_EFR_BIT(REG, BIT)          (efr_read(REG) & (BIT))
#define CLEAR_EFR_REG(REG)              (efr_write(REG, 0x0))
#define WRITE_EFR_REG(REG, VAL)         (efr_write(REG, VAL))


uint8_t efr_read(uint8_t addr);
void efr_io_output(uint8_t io, bool high_low);
void efr_write(uint8_t addr, uint8_t wr_dt);
void efr_bit_change(uint8_t addr, uint8_t start_bit, uint8_t value, bool set_or_reset);
void efr_set_bit(uint8_t addr, uint8_t bitmask);
void efr_clear_bit(uint8_t addr, uint8_t bitmask);
void efr_toggle_bit(uint8_t addr, uint8_t bitmask);
void efr_change(uint8_t addr, uint8_t clearmask, uint8_t setmask);
#endif